package com.cloud.workTest;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OdspTest {

    public static void main(String[] args) throws SQLException {

        Connection conn = DriverManager.getConnection(
                "jdbc:odps:https://service.cn-hangzhou.maxcompute.aliyun.com/api?project=df_ch_100935&useProjectTimeZone=true",
                "LTAI5tFKQUiDtZrrioE85MUT", "jNSQevwtROFzg6dYX3XTg4EeftsiSY");


        // create a table
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("select * from 7b3954764bbe4ae580b013eac085ed84");
        List<Map<String, Object>> list = new ArrayList<>();


        ResultSetMetaData md = rs.getMetaData();
        int colCount = md.getColumnCount();
        String[] colArr = new String[colCount];
        for (int i = 1; i <= colCount; i++) {
            colArr[i - 1] = md.getColumnLabel(i);
        }
        while (rs.next()) {
            Map<String, Object> map = new HashMap<>();
            for (int i = 1; i <= colCount; i++) {
                map.put(colArr[i - 1], rs.getObject(i));
            }
            list.add(map);
        }
        // get meta data
//        DatabaseMetaData metaData = conn.getMetaData();
//        System.out.println("product = " + metaData.getDatabaseProductName());
//        System.out.println("jdbc version = "
//                + metaData.getDriverMajorVersion() + ", "
//                + metaData.getDriverMinorVersion());
//        ResultSet tables = metaData.getTables(null, null, tableName, null);
//        while (tables.next()) {
//            String name = tables.getString("TABLE_NAME");
//            System.out.println("inspecting table: " + name);
//            ResultSet columns = metaData.getColumns(null, null, name, null);
//            while (columns.next()) {
//                System.out.println(
//                        columns.getString("COLUMN_NAME") + "\t" +
//                                columns.getString("TYPE_NAME") + "(" +
//                                columns.getInt("DATA_TYPE") + ")");
//            }
//            columns.close();
//        }

        stmt.close();
        conn.close();
    }
}
